package com.bugsnag.ohos;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * In order to understand what happened in your application before each crash, it can be helpful
 * to leave short log statements that we call breadcrumbs. Breadcrumbs are
 * attached to a crash to help diagnose what events lead to the error.
 *
 * @since 2021-07-01
 */
public class BreadcrumbInternal implements JsonStream.Streamable {
    String message;
    BreadcrumbType type;
    Map<String, Object> metadata;
    Date timestamp;

    /**
     * constructor
     *
     * @param message
     * @param type
     * @param metadata
     * @param timestamp
     */
    public BreadcrumbInternal(String message, BreadcrumbType type,
                              Map<String, Object> metadata, Date timestamp) {
        this.message = message;
        this.type = type;
        this.metadata = metadata;
        this.timestamp = timestamp;
    }

    /**
     * constructor
     *
     * @param message
     */
    public BreadcrumbInternal(String message) {
        this(message, BreadcrumbType.MANUAL,
            new HashMap<>(), new Date());
    }

    @Override
    public void toStream(JsonStream writer) throws IOException {
        writer.beginObject();
        writer.name("timestamp").value(timestamp);
        writer.name("name").value(message);
        writer.name("type").value(type.toString());
        writer.name("metaData");
        writer.value(metadata, true);
        writer.endObject();
    }
}
